package com.rongzhe.house.presenter;

import android.content.Intent;

import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.fragment.MyFavoriteFragment;

import java.util.List;

/**
 * Created by huang on 2017/12/10.
 */

public class MyFavoritePresenter extends BasePresenter {
    private PageContoller<HouseOverview> mPageController;

    public MyFavoritePresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        if (UserManager.getInstance().isLogin()) {
            mPageController = new PageContoller<HouseOverview>(
                    (MyFavoriteFragment) getUiControlInterface());
            loadStaredList(false);
        }
    }

    public void loadStaredList(final boolean append) {
        HouseManager.getInstance().getStaredHouse(mPageController.getMaxDataOnePage(),
                mPageController.getCurrentPage(),
                new DataListener<List<HouseOverview>>() {
                    @Override
                    public void onSuccess(List<HouseOverview> data, String message) {
                        mPageController.onDataLoaded(data, append);
                        if (mPageController.getCurrentPage() == 1) {
                            boolean hasData = data != null && data.size() != 0;
                            ((MyFavoriteFragment) getUiControlInterface()).setEmptyViewShowing(!hasData);
                        }
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                    }

                    @Override
                    public void onEnd() {
                        mPageController.onDataLoadFinish(append);
                    }
                });
    }
}
